package com.huluobo.h3kotlindemo.home

import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.huluobo.h3kotlindemo.R
import com.huluobo.h3kotlindemo.base.BaseMVVMFragment
import com.huluobo.h3kotlindemo.databinding.FragmentHomeBinding
import com.huluobo.h3kotlindemo.detail.DetailActivity
import com.huluobo.h3kotlindemo.net.Const
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class HomeFragment : BaseMVVMFragment<FragmentHomeBinding>() {

    private lateinit var viewModel: HomeViewModel

    private lateinit var homeListAdapter: HomeAdapter

    private val goodsList = mutableListOf<HomeListBean>()
    private val bannerList = mutableListOf<String>()

    private var currentPage = 1
    private var pageSize = 10
    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun initData() {
        viewModel = ViewModelProvider(this)[HomeViewModel::class.java]

        viewModel.getHomeList(0, currentPage, pageSize)
        viewModel.getHomeBanner()

        viewModel.getHomeListSuccess.observe(this) {
            if (it.code == 200) {
                if (currentPage == 1) {
                    goodsList.clear()
                }

                dataBinding.homeRefreshLayout.finishRefresh()
                dataBinding.homeRefreshLayout.finishLoadMore()

                goodsList.addAll(it.data)
                homeListAdapter.notifyDataSetChanged()
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.getHomeBannerSuccess.observe(this) {
            if (it.code == 200) {
                bannerList.clear()
                it.data.forEach {
                    bannerList.add(it.imagePath)
                }
                dataBinding.homeBanner.setImages(bannerList)
                dataBinding.homeBanner.start()
            } else {
                ToastUtils.showLong(it.message)
            }
        }
    }

    override fun initView() {
        dataBinding.homeRv.layoutManager = GridLayoutManager(context, 2)
        homeListAdapter = HomeAdapter(goodsList)
        dataBinding.homeRv.adapter = homeListAdapter

        homeListAdapter.setOnItemClickListener { _, _, position ->
            val intent = Intent(requireActivity(), DetailActivity::class.java)
            intent.putExtra(Const.PARAM_GOODS_ID, goodsList[position].id)
            startActivity(intent)
        }

        dataBinding.homeRefreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                if (refreshLayout.isRefreshing) {
                    refreshLayout.finishRefresh()
                }

                currentPage = 1
                viewModel.getHomeList(0, currentPage, pageSize)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (refreshLayout.isLoading) {
                    refreshLayout.finishLoadMore()
                }
                currentPage += 1
                viewModel.getHomeList(0, currentPage, pageSize)
            }
        })

        dataBinding.homeBanner.setBannerStyle(BannerConfig.NUM_INDICATOR)
        dataBinding.homeBanner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                if (context != null && imageView != null) {
                    Glide.with(context).load(path).into(imageView)
                }
            }
        })
        dataBinding.homeBanner.setDelayTime(1500)
        dataBinding.homeBanner.setIndicatorGravity(BannerConfig.CENTER)

    }
}